FROM openresty:1.27.1.2 AS deps

ARG LUAROCKS_VERSION=3.8.0
ARG LUAJIT_PREFIX="/usr/local/openresty/luajit"

RUN set -eux; \
    dnf install -y gcc make libtool git unzip libyaml-devel pcre-devel && dnf clean all && \
    cd /usr/lib64 && \
    ln -sf libyaml-0.so.2 libyaml.so

COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY deps /deps
COPY luarocks-v${LUAROCKS_VERSION}.tar.gz /luarocks.tar.gz

RUN set -eux; \
    mkdir /luarocks-src && \
    tar zxf /luarocks.tar.gz -C /luarocks-src --strip-components 1 && \
    cd /luarocks-src && \
    ./configure \
        --prefix=${LUAJIT_PREFIX} \
        --with-lua=${LUAJIT_PREFIX} && \
    make -j$(nproc) && \
    make install && \
    ln -sf ${LUAJIT_PREFIX}/bin/luarocks /usr/local/bin/luarocks

RUN set -eux; \
    cd /deps && \
    sh install.sh && \
    cd /usr/local && \
    tar zcf /openresty-deps.tar.gz openresty

FROM openresty:1.27.1.2

ARG APISIX_VERSION=3.12.0
ARG APISIX_PREFIX="/usr/local/apisix"

RUN set -eux; \
    dnf install -y libyaml pcre && dnf clean all && \
    cd /usr/lib64 && \
    ln -sf libyaml-0.so.2 libyaml.so

COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY --from=deps /openresty-deps.tar.gz /openresty-deps.tar.gz

RUN set -eux; \
    tar zxf /openresty-deps.tar.gz -C /usr/local && \
    rm -rf /openresty-deps.tar.gz

RUN set -eux; \
    mkdir ${APISIX_PREFIX} && \
    curl -O https://mirror.tuna.tsinghua.edu.cn/apache/apisix/${APISIX_VERSION}/apache-apisix-${APISIX_VERSION}-src.tgz && \
    tar zxf apache-apisix-${APISIX_VERSION}-src.tgz -C ${APISIX_PREFIX} --strip-components=1 && \
    ln -sf ${APISIX_PREFIX}/bin/apisix /usr/local/bin/apisix && \
    rm -rf apache-apisix-${APISIX_VERSION}-src.tgz

RUN set -eux; \
    userdel -r openresty && rm -rf /home/openresty; \
    groupadd -g 70 apisix; \
    useradd -r -u 70 -g apisix -m -d /home/apisix apisix; \
    mkdir -p /home/apisix/instance; \
    chown -R apisix:apisix /home/apisix/instance /usr/local/apisix

COPY conf/config.yaml /home/apisix/config.yaml.template

ENV LANG en_US.UTF-8
EXPOSE 9180 9080 9091 9443
VOLUME ["/home/apisix/instance"]

COPY entrypoint.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh; \
    ln -sf /usr/local/bin/entrypoint.sh / # backwards compat

ENTRYPOINT ["entrypoint.sh"]
CMD ["apisix", "start", "-c", "/home/apisix/instance/config.yaml"]

STOPSIGNAL SIGQUIT